<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\common\model\Attribute as AttributeModel;
use think\facade\View;
use think\response\Json;

class Attribute extends AdminBase
{
    public function lists(): string
    {
        return View::fetch();
    }

    public function list(): Json
    {
        $page              = $this->request->param('page', 1, 'intval');
        $limit             = $this->request->param('limit', 10, 'intval');
        $name              = $this->request->param('name', '', 'trim');
        $status            = $this->request->param('status', 0, 'intval');
        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);

        $where = [];
        if ($name) {
            $where[] = ['name', 'like', '%' . $name . '%'];
        }
        if ($status) {
            $where[] = ['status', '=', $status];
        }
        $attribute = $attributeBusiness->getAttributeList($page, $limit, $where);
        return showList(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success', $attribute['rows'], $attribute['count']);
    }

    public function add(): string
    {
        return View::fetch();
    }

    public function doAdd(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $name          = $this->request->param('name', '', 'trim');
        $attributeType = $this->request->post('attribute_type', 0, 'intval');
        $isSales       = $this->request->get('is_sales', 0, "intval");
        $status        = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'           => $name,
            'status'         => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'attribute_type' => $attributeType,
            'is_sales'       => $isSales === AttributeModel::IS_SALES_ATTRIBUTE ? AttributeModel::IS_SALES_ATTRIBUTE : AttributeModel::IS_NORMAL_ATTRIBUTE,
        ];
        $validate = (new \app\admin\validate\Attribute())->scene('add');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $res               = $attributeBusiness->addAttribute($data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function edit(): string
    {
        $id = $this->request->param('id', 0, 'intval');
        if (!$id) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $attribute         = $attributeBusiness->findAttributeById($id);
        if (empty($attribute)) {
            View::assign(['msg' => self::_PARAMETER_ERROR_MESSAGE]);
            return View::fetch('permission/msg');
        }
        View::assign(["attribute" => $attribute]);

        return View::fetch();
    }

    public function doEdit(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id            = $this->request->param('id', 0, 'intval');
        $name          = $this->request->param('name', '', 'trim');
        $attributeType = $this->request->post('attribute_type', 0, 'intval');
        $isSales       = $this->request->get('is_sales', 0, "intval");
        $status        = $this->request->param('status', 0, 'intval');

        $data     = [
            'name'           => $name,
            'status'         => $status == self::_IS_ENABLE_STATUS ? self::_IS_ENABLE_STATUS : self::_IS_DISABLE_STATUS,
            'attribute_type' => $attributeType,
            'is_sales'       => $isSales === AttributeModel::IS_SALES_ATTRIBUTE ? AttributeModel::IS_SALES_ATTRIBUTE : AttributeModel::IS_NORMAL_ATTRIBUTE,
        ];
        $validate = (new \app\admin\validate\Attribute())->scene('edit');
        if (!$validate->check($data)) {
            return show(config("status.error"), $validate->getError());
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $res               = $attributeBusiness->updateAttribute($id, $data);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doDelete(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $id = $this->request->param('id', 0, 'intval');
        if ($id == 0) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_PARAMETER_ERROR_MESSAGE);
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $res               = $attributeBusiness->deleteAttribute($id);
        if ($res[self::_RESULT]) {
            return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), $res[self::_MESSAGE]);
        }

        return show(config(self::_CONFIG_STATUS_ERROR_CODE), $res[self::_MESSAGE]);
    }

    public function doCache(): Json
    {
        if (!$this->request->isPost()) {
            return show(config(self::_CONFIG_STATUS_ERROR_CODE), self::_REQUEST_WAY_ERROR_MESSAGE);
        }

        $attributeBusiness = new \app\common\business\Attribute($this->websiteCode);
        $attributeBusiness->clearAttributeCache();

        $attributeValueBusiness = new \app\common\business\AttributeValue($this->websiteCode);
        $attributeValueBusiness->clearAttributeValueCache();

        return show(config(self::_CONFIG_STATUS_SUCCESS_CODE), 'success');
    }
}
